
from django.core.exceptions import ObjectDoesNotExist

from api.models import MemberLevel, Material, MemberLevel2Material, BackRole, BackUser, Member
from api.serializers import MemberLevelSerializer, MaterialSerializer, BackRoleSerializer, MemberSerializer
from api.utils_material import material_type_name_zh,get_material_subtype


def member_level_exist(level_code):
    try:
        rows = MemberLevel.objects.filter(code=level_code)
        if len(rows) == 0:
            return False
        # print('package rows len:', len(rows))
        return True
    except ObjectDoesNotExist:
        return False
    return False


def member_exist(member_name):
    try:
        rows = Member.objects.filter(name=member_name)
        if len(rows) == 0:
            return False
        return True
    except ObjectDoesNotExist:
        return False
    return False


def get_member_by_wechat_openid(wechat_openid):
    try:
        row = Member.objects.get(wechat_openid=wechat_openid)
        return MemberSerializer(row).data
    except ObjectDoesNotExist:
        return {}
    return {}


def user_exist(username):
    try:
        rows = BackUser.objects.filter(name=username)
        if len(rows) == 0:
            return False
        # print('user rows len:', len(rows))
        return True
    except ObjectDoesNotExist:
        return False
    return False


def member2material_exist(level_code, material_id):
    try:
        rows = MemberLevel2Material.objects.filter(level_code=level_code).filter(material_id=material_id)
        if len(rows) == 0:
            return False
        # print('package2material rows len:', len(rows))
        return True
    except ObjectDoesNotExist:
        return False
    return False


def get_material(material_id):
    try:
        row = Material.objects.get(id=material_id)
        material_data = MaterialSerializer(row).data
        if len(material_data) > 0:
            material_type = material_data['material_type']
            material_subtype = material_data['material_subtype']
            subtypeinfo = get_material_subtype(material_type, material_subtype)
            material_data['material_type_name_zh'] = material_type_name_zh(material_type)
            material_data['material_subtype_name_zh'] = subtypeinfo.get('name_zh','')
            return material_data
        else:
            return []
    except ObjectDoesNotExist:
        return {}
    return {}


def get_memberlevel(level_code):
    try:
        row = MemberLevel.objects.get(code=level_code)
        return MemberLevelSerializer(row).data
    except ObjectDoesNotExist:
        return {}
    return {}


def m_ids_in_memberlevel2material(level_code, material_type):
    def get_ids_in_related_table(level_code):
        try:
            rows = MemberLevel2Material.objects.filter(level_code=level_code)
            ids = []
            for row in rows:
                ids.append(row.id)
            return ids

        except ObjectDoesNotExist:
            return []
        return []

    def filter_onetype_ids_in_material(m_ids, material_type):
        try:
            rows = Material.objects.filter(id__in=m_ids).filter(material_type=material_type)
            print(rows.query)
            ids = []
            for row in rows:
                ids.append(row.id)
            return ids

        except ObjectDoesNotExist:
            return []
        return []
    m_ids = get_ids_in_related_table(level_code)
    print(f" 1 m_ids is {m_ids}")
    m_ids = filter_onetype_ids_in_material(m_ids, material_type)
    print(f" 2 m_ids is {m_ids}")
    return m_ids


def get_role(role_id):
    try:
        rows = BackRole.objects.filter(id=role_id)
        print('backrole query', rows.query)
        if len(rows) == 0:
            print('len(rows) == 0 query')
            return {}
        return BackRoleSerializer(rows[0]).data
    except ObjectDoesNotExist:
        return {}
    return {}
